<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="status2List" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="设备编号" align="center" prop="equipmentId"/>
      <el-table-column label="设备名称" align="center" prop="equipmentName"/>
      <el-table-column label="设备状态" align="center" prop="status"/>
      <!--      <el-table-column label="是否需报修" align="center" prop="needsRepair"/>-->
      <!--      <el-table-column label="备注信息" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="addRepair(scope.row)"
          >报修
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改故障报修管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="120px">

        <!-- 新增的表单项 -->
        <el-form-item label="设备ID" prop="deviceName">
          <el-input v-model="form1.deviceId" :disabled="true"/>
        </el-form-item>
        <el-form-item label="设备完整名称" prop="deviceName">
          <el-input v-model="form1.deviceName" :disabled="true"/>
        </el-form-item>

        <el-form-item label="设备当前位置" prop="location">
          <el-input v-model="form1.location" placeholder="请输入设备当前位置"/>
        </el-form-item>
        <!--        <el-form-item label="报修人员工号" prop="reporterId">-->
        <!--          <el-input v-model="form1.reporterId" placeholder="请输入报修人员工号"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="故障情况" prop="remark">
          <el-input v-model="form1.remark" placeholder="请输入故障情况"/>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form1.status" placeholder="请选择处理状态">
            <el-option label="未处理" value="未处理"></el-option>
            <el-option label="处理中" value="处理中"></el-option>
            <el-option label="已处理" value="已处理"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {delStatus2, getStatus2, listStatus2, updateStatus2} from "@/api/system/status2";
import {addManagement1} from "@/api/system/management1";

export default {
  name: "Status2",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备状态表格数据
      status2List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        needsRepair: null,
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      // rules: {
      //   equipmentName: [
      //     {required: true, message: "设备名称不能为空", trigger: "blur"}
      //   ],
      //   needsRepair: [
      //     {required: true, message: "是否需报修不能为空", trigger: "blur"}
      //   ],
      //   updateTime: [
      //     {required: true, message: "最后更新时间不能为空", trigger: "blur"}
      //   ],
      //   updateBy: [
      //     {required: true, message: "最后更新人不能为空", trigger: "blur"}
      //   ],
      //   createTime: [
      //     {required: true, message: "创建时间不能为空", trigger: "blur"}
      //   ],
      //   createBy: [
      //     {required: true, message: "创建人不能为空", trigger: "blur"}
      //   ]
      // }      // 表单校验
      rules: {
        deviceId: [
          {required: true, message: "关联设备编号不能为空", trigger: "blur"}
        ],
        deviceName: [
          {required: true, message: "设备完整名称不能为空", trigger: "blur"}
        ],

        repairReason: [
          {required: true, message: "详细故障描述不能为空", trigger: "blur"}
        ],
        location: [
          {required: true, message: "设备当前位置不能为空", trigger: "blur"}
        ], remark: [
          {required: true, message: "故障情况", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备状态列表 */
    getList() {
      this.loading = true;
      listStatus2(this.queryParams).then(response => {
        this.status2List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        equipmentId: null,
        equipmentName: null,
        status: null,
        needsRepair: null,
        remark: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },    // 表单重置
    reset1() {
      this.form1 = {
        repairCode: null,
        deviceId: null,
        deviceName: null,
        deviceType: null,
        repairReason: null,
        location: null,
        registerTime: null,
        reporterId: null,
        reporterName: null,
        status: null,
        remark: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.equipmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备状态";
    },
    addRepair(row) {
      this.reset1()
      this.form1.deviceName = row.equipmentName
      this.form1.deviceId = row.equipmentId
      this.form.equipmentId = row.equipmentId
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.reset1();
      const equipmentId = row.equipmentId || this.ids
      getStatus2(equipmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备状态";
      });
    },
    /** 提交按钮 */
    /** 提交按钮 */
    submitForm() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          addManagement1(this.form1).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
        updateStatus2(this.form).then(response => {

        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipmentIds = row.equipmentId || this.ids;
      this.$modal.confirm('是否确认删除设备状态编号为"' + equipmentIds + '"的数据项？').then(function () {
        return delStatus2(equipmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/status2/export', {
        ...this.queryParams
      }, `status2_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
